<!-- Web Upload Data Delete PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/06/08 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. 增加 FTP 上 Delete File 功能, 及是否要 delete 勾选
		3. 可删除 MySQL 上, 不同站别, 但相同版本及描述
	Reb 1.08 at 2015/12/08 by Max Yin,
		1. Web SMT upload 增加-回流焊的选项。(eric_liang)
		2. Web SMT upload/download, 由填入栏位改为下拉选单。(eric_liang)
		3. Web Upload/Downlaod 新增加 ESD 下拉选单。(jianwei_wang)
		4. 移除 ftp IP 的栏位输入, 由 Web 程式内定 FTP 路径。(Lucas)
		5. 修正重复上传, 却只有版本不对, 导致无法上传问题。(mingshen_feng)
		6. 修正若上传者, 若没有上传者名称, 会要求重新登入讯息。
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta charset="UTF-8">
	</head>
	<body>
		<center>
			<?php
				date_default_timezone_set('PRC');//设置当前时间为中国时间
				require_once('class_package/common.php');
				// $tmp_pn = CRL615766000A,CRL615766007A,...
				$tmp_pn			= strtoupper($_POST['webul_pn']);
				$tmp_wsid		= strtoupper($_POST['webul_wsid']);
				$tmp_tpver		= strtoupper($_POST['webul_mfg_ver']);
				$tmp_work_order	= strtoupper($_POST['webul_work_order']);
				$tmp_line		= strtoupper($_POST['webul_line']);
				$tmp_panel		= strtoupper($_POST['webul_panel']);
				$tmp_by				= $_COOKIE['login_user'];
				$tmp_ftpfile        = "N";
				if ( !empty($_POST['ftp_file_del_chk']) ){ 
					$del_ftpfile_chk = 1; 					
				}else {$del_ftpfile_chk = 0;}
				// echo "each_pn = $each_pn<br>";
				// echo "tmp_wsid = $tmp_wsid<br>";
				// echo "tmp_tpver = $tmp_tpver<br>";
				if ( strlen($tmp_wsid) == 0 || strlen($tmp_wsid) > 60 ) {
					echo "<script>alert('输入的-WSID(站別)或其他栏位中, 不能为空值或超过60个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_tpver) == 0 || strlen($tmp_tpver) > 30 ) {
					echo "<script>alert('输入的-TPVER(程式版本)或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				// if ( strpos($tmp_tpver,' ') > -1 ) {
					// echo "<script>alert('输入的TPVER(程式版本)的栏位中, 不能为\' \'空白,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
					// exit();
				// }
				if ( strlen($tmp_line) == 0 || strlen($tmp_line) > 30 ) {
					echo "<script>alert('输入的-线别Line或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_panel) == 0 || strlen($tmp_panel) > 30 ) {
					echo "<script>alert('输入的-板面Panel或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_wsid,'SMT') > -1  ) {
					if ( $tmp_line == 'N/A' ) {
						echo "<script>alert('输入的栏位中,线别Line,不能为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
					
					if ( $tmp_panel == 'N/A' ) {
						echo "<script>alert('输入的栏位中,板面Panel,不能为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				if ( strpos($tmp_wsid,'SMT') > -1 || strpos($tmp_wsid,'ICT') > -1 || strpos($tmp_wsid,'TOOL') > -1 || strpos($tmp_wsid,'SCT') > -1 || strpos($tmp_wsid,'H/P') > -1 || strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
					if ( $tmp_work_order != 'N/A' ) {
						echo "<script>alert('输入的-Work Order(工單)应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				} else {
					if ( $tmp_work_order == 'N/A' ) {
						echo "<script>alert('输入的-Work Order(工單)不应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				
				unset($get_pn_array);
				if ( strpos($tmp_pn,',') > -1 ) {					
					$get_pn_array = preg_split("/[\s,]+/",$tmp_pn);
				} else {
					$get_pn_array[] = $tmp_pn;
				}
				
				foreach ( $get_pn_array as $each_pn ) {
					// To check user name & password limit ...
					if ( strlen($each_pn) == 0 || strlen($each_pn) > 13 ) {
						echo "<script>alert('输入的-Part Number(局部料号), 逗号\',\'后, 不能为空值或超过13个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 || strpos($tmp_wsid,'TOOL') > -1 ) {
						if ( $each_pn != 'N/A' ) {
							echo "<script>alert('输入的栏位中,Part Number(局部料号)不为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					} else {
						if ( strlen($each_pn) != 13 ) {
							echo "<script>alert('输入的查询栏位中-Part Number(局部料号),应该为 13 个字元,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					}
					
					//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
					unset($get_wsid_array);
					if ( strpos($tmp_wsid,',') > -1 )					
						$get_wsid_array = preg_split("/[\s,]+/",$tmp_wsid);
					else
						$get_wsid_array[] = $tmp_wsid;
					
					$Station_Obj = new Stations();
					$err_chk     = '';
					$db_select   = Sql_Download::sql_db;
					$db_table    = Sql_Download::sql_table;
					$table_name1 = Sql_Download::table_name1;	//'webdl_pn
					$table_name2 = Sql_Download::table_name2;	//'webdl_wsid';
					$table_name3 = Sql_Download::table_name3;	//'webdl_tpver
					$table_name4 = Sql_Download::table_name4;	//'webdl_work_order';
					$table_name5 = Sql_Download::table_name5;	//'webdl_line';
					$table_name6 = Sql_Download::table_name6;	//'webdl_panel';
					$FileDelChk  = 0;
					foreach ( $get_wsid_array as $each_wsid ) {
						// To check the WSID is exist ...
						$get_StationChk = $Station_Obj->Station_Exist($each_wsid);
						if ( $get_StationChk == 0 ) {
							$err_chk = 'Station_not_exist';
							break;
						}

						// Check connection
						$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
						if ( mysqli_connect_errno($conn) ) { 
							echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
							exit();
						}
						mysqli_set_charset($conn,'set names utf8');
						
						// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$each_pn' AND `$table_name2`='$each_wsid' AND `$table_name3`='$tmp_tpver' AND `$table_name4`='$tmp_work_order' AND `$table_name5`='$tmp_line' AND `$table_name6`='$tmp_panel'";
						$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
						if ( !$result ) { 
							mysqli_close($conn);				// 关闭资料库
							echo "<script>alert('MySql资料库搜寻失败, 请跟开发人员查明原因...');history.go(-1);</script>";
							exit();
						}
						mysqli_close($conn);					// 关闭资料库
						
						$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
						if ( $rows == 0 ) { 
							echo "<script>alert('该 P/N 与站别, 在资料库查询不到相关资讯, 请查询后再删除...');history.go(-1);</script>";
							exit();
						}
						
						$each_ftpipfile = '';
						while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
							$each_ftpipfile = $getArray['webdl_ftpip'];
							$creator = $getArray['webdl_creator'];
						}
						
						if ( $each_ftpipfile == NULL ) {
							echo "<script>alert('在资料库获取不到相关该笔删除资料的FTP文件IP, 请查询后再删除...');history.go(-1);</script>";
							exit();
						}
							
						if ( $del_ftpfile_chk == 1 ) {
							// delete ftp old file ...
							if(!file_exists($each_ftpipfile)){
								echo "<script>alert('ftp文件删除失败！文件不存在！请联系管理人员！');history.go(-1);</script>";
							}
							if ( $FileDelChk == 0 ) {
								$RELST=unlink($each_ftpipfile);
								if(!$RELST){
									echo "<script>alert('ftp文件删除失败！请联系管理人员！');history.go(-1);</script>";
								}
								$tmp_ftpfile     = "Y";
								if ( strpos($each_ftpipfile,'COMMON') > -1 ) $FileDelChk = 1;
							}
						}

						// To delete the account by user name from MySQL ...
						$result = download_delete($each_pn,$each_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel);				
						if ( !$result ) {
							$err_chk = 'Delete_Fail';
							break;
						}
						
						//删除成功在这里记录：删除的人，pn,wsid， 时间，版本，工单，创建的人程式的人
						$deleteTime =date('Y-m-d H:i:s', time());
						$deleteUser=$tmp_by;	//登录/删除人	
						$re=download_deleted_record($each_pn,$tmp_work_order,$tmp_tpver,$each_wsid,$creator,$deleteUser,$deleteTime,$tmp_ftpfile);//执行插入记录
						if($re!=1){
							echo "<script>alert('该删除未被记录，但已经删除，请留存资料并系统联系管理人员！');</script>";
						}
					}
					
					if ( $err_chk == 'Station_not_exist' ) {
						echo "<script>alert('在 WSID(站別) 栏位中, 有不存在的站别, 请重新再输入一次!!');history.go(-1);</script>";
						exit();
					} elseif ( $err_chk == 'Delete_Fail' ) {
						echo "<script>alert('该程式, 删除失败! 请联系程式开发人员, 并查明原因...');history.go(-1);</script>";
						exit();
					}
				}
				echo "<script>alert('指定程式, 都已删除成功!');history.go(-3);</script>";
				exit();
			?>
		</center>
	</body>
</html>

